<?php

namespace App\Admin\Controllers;

use App\Models\SwtPledgeOrder;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class SwtPledgeOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(SwtPledgeOrder::with(['user','trade','pledge']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user.name','用户');
            $grid->column('trade.chain_name','币种');
            $grid->column('pledge.name','项目');
            $grid->column('order_no');
            $grid->column('total');
            $grid->column('year_rate');
            $grid->column('lock_day');
            $grid->column('income_amount');
//            $grid->column('sign_status') -> using(['未签约','已签约']) -> label(['danger', 'success']);
            $grid->column('status') -> using(['待生效','已生效','已赎回','已到期','取消'])->label([
                0 => 'primary',
                1 => 'success',
                2 => 'warning',
                3 => Admin::color()->info(),
                4 => 'danger',
            ]);

            $grid->column('begin_time','有效期') -> display(function () {
                return $this->start_time ? date('Y-m-d', strtotime($this->start_time)) . '至'. date('Y-m-d', strtotime($this->end_time)) : '-';
            });

            $grid->column('created_at');

            $grid->disableCreateButton();
            $grid->disableActions();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new SwtPledgeOrder(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('trade_id');
            $show->field('pledge_id');
            $show->field('rule_id');
            $show->field('order_no');
            $show->field('total');
            $show->field('year_rate');
            $show->field('lock_day');
            $show->field('revenue_to_escrow');
            $show->field('income_amount');
            $show->field('sign_status');
            $show->field('status');
            $show->field('start_time');
            $show->field('end_time');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new SwtPledgeOrder(), function (Form $form) {
            $form->display('id');
            $form->display('order_no');
            $form->display('total');
            $form->display('year_rate');
            $form->display('lock_day');

            $form->radio('sign_status') -> options(
                [
                    '未签约',
                    '已签约'
                ]
            );
            $form -> hidden('start_time');
            $form -> hidden('end_time');
            $form -> hidden('lock_day');
            $form -> hidden('status');

            $form -> saving(
                function (Form $form) {
                    if($form -> sign_status == 1) {
                        $form -> start_time = date('Y-m-d H:i:s', time());
                        $form -> end_time = date('Y-m-d H:i:s', strtotime('+ '. $form -> lock_day.'days'));
                        $form -> status = 1;
                    }
                }
            );
        });
    }
}
